<?php

class BaseLoDe
{
    protected $MAX_CHOICE = 100;
    protected $MIN_VALUE = '00';
    protected $MAX_VALUE = '99';
    
    // Loại game
    protected $typeGame = Constant::GAME_LODE_ABSTRACT_TYPE;
    
    // Tiền đặt cược
    var $bet = 0;
    var $arrChoices = array();
    var $gameName = '';
    var $rationMoney = 1;
    
    function setBet($bet)
    {
        $this->bet = $bet;
    }
    
    function setChoices($choices)
    {
        $this->arrChoices = $choices;
    }
    
    function getGameName()
    {
        return $this->gameName;
    }
    
    public function validateInputs()
    {
        if(count($this->arrChoices) <= $this->MAX_CHOICE)
        {
            $valid = TRUE;
            foreach ($this->arrChoices as $choice)
            {
                if((string)$choice < $this->MIN_VALUE || (string)$choice > $this->MAX_VALUE)
                {
                    $valid = FALSE;
                }
            }
            
            return $valid;
        }
        return FALSE;
    }
    
    public function saveDB()
    {
        $lode = new LoDe();
        $lode->choices = json_encode($this->arrChoices);
        $lode->bet = $this->bet;
        //$lode->bet = $this->calculateRequiredAmount();
        $lode->lode_type = $this->typeGame;
        $lode->play_date = date('Y-m-d H:i:s');
        $lode->player_id = Yii::app()->user->id;
        return $lode->save();
    }
    
    function play()
    {
        if($this->validateInput())
        {
            return $this->saveDB();
        }
        
        return FALSE;
    }
    
    function calculateRequiredAmount()
    {
        return count($this->arrChoices)*$this->bet;
    }
    
    function calculateWinMoney($ketquaXoSo)
    {
        return 0;
    }
}

?>
